cmake_minimum_required(VERSION 3.2)
project(ddm)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include_directories(${CMAKE_SOURCE_DIR})

set(CMAKE_MODULE_PATH
  "${CMAKE_SOURCE_DIR}/build/fbcode_builder/CMake"
  "${CMAKE_SOURCE_DIR}"
  ${CMAKE_MODULE_PATH}
)

include(FBThriftCppLibrary)
# include(FBGenCMakeBuildInfo)

set(REQ_BOOST_COMPONENTS ${REQ_BOOST_COMPONENTS} system thread context filesystem program_options regex)

find_library(ASYNC async PATHS)
find_package(Boost REQUIRED COMPONENTS ${REQ_BOOST_COMPONENTS})
find_library(CONCURRENCY concurrency PATHS)
find_library(DOUBLE-CONVERSION double-conversion)
find_package(folly REQUIRED)
find_package(fb303 CONFIG REQUIRED)
find_package(fizz REQUIRED)
find_package(fmt REQUIRED)
find_package(Gflags REQUIRED)
find_package(Glog REQUIRED)
find_library(RE2 re2)
find_library(SODIUM sodium)
find_package(FBThrift REQUIRED)
find_package(wangle REQUIRED)
find_package(Threads REQUIRED)
find_library(ZSTD zstd)
find_library(BENCHMARK follybenchmark PATHS)
find_package(range-v3 REQUIRED)

find_path(RE2_INCLUDE_DIR re2/re2.h)

find_library(OPENRLIB openrlib REQUIRED)
find_library(openr_ctrl_cpp_cpp2 openr_ctrl_cpp_cpp2 REQUIRED)
find_library(openr_ctrl_cpp2 openr_ctrl_cpp2 REQUIRED)
find_library(kv_store_cpp2 kv_store_cpp2 REQUIRED)
find_library(openr_config_cpp2 openr_config_cpp2 REQUIRED)
find_library(bgp_config_cpp2 bgp_config_cpp2 REQUIRED)
find_library(vip_service_config_cpp2 vip_service_config_cpp2 REQUIRED)
find_library(routing_policy_cpp2 routing_policy_cpp2 REQUIRED)
find_library(network_cpp2 network_cpp2 REQUIRED)
find_library(types_cpp2 types_cpp2 REQUIRED)

# Openr do not have cmake generated, find openr include library by searching file
find_path(OPENR_INCLUDE_DIR openr/common/AsyncDebounce.h)

include_directories(
  ${FOLLY_INCLUDE_DIR}
  ${FBTHRIFT_INCLUDE_DIR}
  # ${WANGLE_INCLUDE_DIR}
  ${FB303_INCLUDE_DIR}
  # ${FIZZ_INCLUDE_DIR}
  ${OPENR_INCLUDE_DIR}
)

add_fbthrift_cpp_library(
  ddm_ctrl_cpp_cpp2
  ddm/if/DDM.thrift
  SERVICES
    DdmCtrl
  OPTIONS
    json
    stream
    server_stream
  DEPENDS
    ${network_cpp2}
)
SET(DDM_THRIFT_LIBS ${DDM_THRIFT_LIBS} ddm_ctrl_cpp_cpp2)

add_executable(ddm_bin
  ddm/AtomicFib.cpp
  ddm/AtomicRoute.cpp
  ddm/CtrlHandler.cpp
  ddm/Main.cpp
  ddm/MapReducer.cpp
  ddm/OpenrAgent.cpp
  ddm/ThriftServer.cpp
  ddm/Util.cpp
)

target_link_libraries(ddm_bin
  ${OPENRLIB}
  ${DDM_THRIFT_LIBS}
  ${GLOG}
  ${GFLAGS}
  ${THRIFT}
  FBThrift::thriftcpp2
  ${ASYNC}
  ${CONCURRENCY}
  Folly::folly
  # ${FOLLY_EXCEPTION_TRACER}
  ${Boost_LIBRARIES}
  ${openr_ctrl_cpp_cpp2}
  ${openr_ctrl_cpp2}
  ${kv_store_cpp2}
  ${openr_config_cpp2}
  ${bgp_config_cpp2}
  ${vip_service_config_cpp2}
  ${routing_policy_cpp2}
  ${network_cpp2}
  ${types_cpp2}
  fb303::fb303_thrift_cpp
  fb303::fb303
  -lpthread
  -lcrypto
  -lre2
)

install(TARGETS
  ddm_bin
  DESTINATION sbin
  RENAME ddm
)

add_executable(kvtest
  ddm/KVTest.cpp
)

target_link_libraries(kvtest
  ${OPENRLIB}
  ${GLOG}
  ${GFLAGS}
  ${THRIFT}
  FBThrift::thriftcpp2
  ${ASYNC}
  ${CONCURRENCY}
  Folly::folly
  # ${FOLLY_EXCEPTION_TRACER}
  ${Boost_LIBRARIES}
  ${openr_ctrl_cpp_cpp2}
  ${openr_ctrl_cpp2}
  ${kv_store_cpp2}
  ${openr_config_cpp2}
  ${bgp_config_cpp2}
  ${vip_service_config_cpp2}
  ${routing_policy_cpp2}
  ${network_cpp2}
  ${types_cpp2}
  fb303::fb303_thrift_cpp
  fb303::fb303
  -lpthread
  -lcrypto
  -lre2
)

add_executable(ddmcli
  ddm/Cli.cpp
)

target_link_libraries(ddmcli
  ${OPENRLIB}
  ${DDM_THRIFT_LIBS}
  ${GLOG}
  ${GFLAGS}
  ${THRIFT}
  FBThrift::thriftcpp2
  ${ASYNC}
  ${CONCURRENCY}
  Folly::folly
  ${Boost_LIBRARIES}
  ${openr_ctrl_cpp_cpp2}
  ${openr_ctrl_cpp2}
  ${kv_store_cpp2}
  ${openr_config_cpp2}
  ${bgp_config_cpp2}
  ${vip_service_config_cpp2}
  ${routing_policy_cpp2}
  ${network_cpp2}
  ${types_cpp2}
  fb303::fb303_thrift_cpp
  fb303::fb303
  -lpthread
  -lre2
)